package com.xywy.bloodandroid.act;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.adapter.FamousDoctorAdapter;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.bean.HotDoctorInfo;
import com.xywy.bloodandroid.util.Constants;
import com.xywy.bloodandroid.util.JsonUtil;
import com.xywy.bloodandroid.util.UIUtil;
import com.xywy.bloodandroid.vollery.GetRequest;
import com.xywy.bloodandroid.vollery.OnVolleyResponseListener;

/**
 * *****************************************  
* @Title: SelectorDoctorAct.java 
* @Package com.xywy.bloodandroid.act 
* @Description: TODO( 根据条件 查询后的专家) 
* @author smz
* @date 2014-12-26 下午2:03:26 
* @version V1.0   
*******************************************
 */
public class SelectorDoctorAct extends BaseActivity implements OnScrollListener{

	private RelativeLayout left_img;
	private TextView middle_title;
	private int pageCount=10;
	private int PageNum=1;
	private List<HotDoctorInfo> list;
	private FamousDoctorAdapter adapter;
	private ListView list_view;
	
	private int visibleLastIndex = 0; //
	private LinearLayout list_footer;
	private TextView tv_msg;
	private LinearLayout loading;
	

	private boolean isLoadMore = true;
	
	protected static final int SUCESS = 1;

	@Override
	protected void initView(Bundle savedInstanceState) {
     setContentView(R.layout.act_selector_doctor);
     left_img = (RelativeLayout) findViewById(R.id.left_img);
     middle_title = (TextView) findViewById(R.id.middle_title);
     
     
     list_view = (ListView) findViewById(R.id.list_view);
     
     
     list_footer = (LinearLayout) getLayoutInflater().inflate(
				R.layout.list_footer, null);
		//
		list_footer.setVisibility(View.GONE);
		tv_msg = (TextView) list_footer.findViewById(R.id.tv_msg);
		loading = (LinearLayout) list_footer.findViewById(R.id.loading);
		list_view.addFooterView(list_footer);
     
     list=new ArrayList<HotDoctorInfo>();	
     adapter = new FamousDoctorAdapter(context, list);
     
     list_view.setAdapter(adapter);
     
       
     
	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(this);
		list_view.setOnScrollListener(this);
	}

	@Override
	protected void initData() {
		province = getIntent().getStringExtra("province");
		title = getIntent().getStringExtra("title");
		keyword = getIntent().getStringExtra("keyword");
		level = getIntent().getStringExtra("level");
		IsPlus = getIntent().getStringExtra("IsPlus");
		if(province==null)
			province="";
		if(title==null)
			title="";
		if(keyword==null)
			keyword="";
		if(level==null)
			level="";
		
		try {
			province = URLEncoder.encode(province, "utf-8");
			title = URLEncoder.encode(title, "utf-8");
			keyword = URLEncoder.encode(keyword, "utf-8");
			level = URLEncoder.encode(level, "utf-8");
			IsPlus = URLEncoder.encode(IsPlus, "utf-8");
			illid = URLEncoder.encode("高血压", "utf-8");
	        getSelectorDoctor(province,title,keyword,level,IsPlus,illid);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
	}
	/**
	 * 根据条件查询的专家列表
	 */
	private void getSelectorDoctor(String province,String title,String keyword,String level,String IsPlus,String illid) {
		final Message msg = Message.obtain();
		isLoadMore = true;
		GetRequest<String> getRequest=new GetRequest<String>(Constants.getUrl("zhuanjia","SearchDoctor")+
				"&pageCount="+pageCount+"&pageNum="+PageNum+"&province="+province
				+"&title="+title+"&keyword="+keyword+"&illid="+illid+"&IsPlus="+IsPlus+"&level="+level
				, String.class, new OnVolleyResponseListener<String>() {
      
			@Override
			public void onError(VolleyError volleyError) {
				UIUtil.DissDialog(context, waittingDialog);
				UIUtil.showToast(context, context.getString(R.string.network_slow));
				
			}

			@Override
			public void onSuccess(String response) {
				UIUtil.DissDialog(context, waittingDialog);
				JSONObject object;
				try {
					object = new JSONObject(response);
				
				int code=object.optInt("code");
				String total=object.optString("total");
				String data=object.optString("data");
				String Msg=object.optString("msg");
				if(code==10000){
				middle_title.setText("全部"+total+"位专家");
				list.addAll((List<HotDoctorInfo>) JsonUtil.parseJsonToList(data,
						new TypeToken<List<HotDoctorInfo>>() {
						}.getType()));
				
				msg.what = SUCESS;
				handler.sendMessage(msg);
				
				
				}else{
					UIUtil.showToast(context, Msg);
				}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		getRequest.setIsParseJson(false);
		executeRequest(getRequest);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.left_img:
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = adapter.getCount(); //
		int lastIndex = itemsLastIndex + 1; //
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
				&& visibleLastIndex == lastIndex) {
			if (isLoadMore) {
				isLoadMore = false;
				PageNum++;
				tv_msg.setVisibility(View.GONE);
				loading.setVisibility(View.VISIBLE);
				getSelectorDoctor(province,title,keyword,level,IsPlus,illid);
			}
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		visibleLastIndex = firstVisibleItem + visibleItemCount;
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case SUCESS:
				if (list.size() > 0 & list.size() <= pageCount * PageNum
						&& pageCount > 0) {
					list_footer.setVisibility(View.VISIBLE);
					tv_msg.setVisibility(View.VISIBLE);
					loading.setVisibility(View.GONE);
				}

				if (list.size() < 10 || list.size() < pageCount * PageNum) {
					list_view.removeFooterView(list_footer);

				}
				adapter.notifyDataSetChanged();

				break;
			
			
			default:
				break;
			}
		}
	};
	private String province;
	private String title;
	private String keyword;
	private String level;
	private String IsPlus;
	private String illid ;
}
